package com.huawei.appmarket.service.thirdupdate;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.view.KeyEvent;
import com.huawei.appgallery.assistantdock.base.externalaction.openviewaction.RemoteBuoyAction;
import com.huawei.appmarket.service.thirdupdate.ThirdUpdateActivityProtocol.Request;
import com.huawei.secure.android.common.intent.SafeIntent;
import o.cep;
import o.cjm;
import o.cjo;
import o.Logger;
import o.dof;
import o.dyr;
import o.dys.a;
import org.json.JSONArray;

public class ThirdAppUpdateAction extends dyr {
    private static final String APP_MUST_UPDATE_BTN = "APP_MUST_UPDATE_BTN";
    private static final String APP_PACKAGENAME = "APP_PACKAGENAME";
    private static final int IS_HIAPP_CAN_SILENT_PROCESS = 6;
    private static final String TAG = "ThirdAppUpdateAction";
    private static final int THIRD_UPDATE_REQUEST_CODE = 1001;

    public ThirdAppUpdateAction(a aVar) {
        super(aVar);
    }

    public void onAction() {
        Intent intent = this.callback.getIntent();
        if (intent != null) {
            SafeIntent safeIntent = new SafeIntent(intent);
            boolean booleanExtra = safeIntent.getBooleanExtra("isHmsOrApkUpgrade", false);
            if (!booleanExtra || cep.ˏ()) {
                String stringExtra = safeIntent.getStringExtra(APP_PACKAGENAME);
                boolean booleanExtra2 = safeIntent.getBooleanExtra(APP_MUST_UPDATE_BTN, false);
                cjo thirdUpdateActivityProtocol = new ThirdUpdateActivityProtocol();
                Request request = new Request();
                request.ˏ(stringExtra);
                request.ˏ(booleanExtra2);
                String stringExtra2 = intent.getStringExtra(RemoteBuoyAction.REMOTE_BUOY_PARAM);
                if (!dof.ˋ(stringExtra2)) {
                    try {
                        JSONArray jSONArray = new JSONArray(stringExtra2);
                        int length = jSONArray.length();
                        String string = jSONArray.getJSONObject(0).getString("pkgName");
                        int i = jSONArray.getJSONObject(0).getInt("versioncode") - 1;
                        String stringExtra3 = safeIntent.getStringExtra("upgradeDlgContent");
                        String stringExtra4 = safeIntent.getStringExtra("buttonDlgY");
                        String stringExtra5 = safeIntent.getStringExtra("buttonDlgN");
                        request.ॱ(string);
                        request.ˊ(i);
                        request.ˊ(stringExtra3);
                        request.ˎ(stringExtra4);
                        request.ʼ(stringExtra5);
                        request.ˋ(booleanExtra);
                        if (length == 2) {
                            int i2 = jSONArray.getJSONObject(1).getInt("versioncode") - 1;
                            request.ˋ(jSONArray.getJSONObject(1).getString("pkgName"));
                            request.ˋ(i2);
                        }
                    } catch (Throwable e) {
                        Logger.ˋ(TAG, "fromJsonArrayStr JSONException: ", e);
                    } catch (Throwable e2) {
                        Logger.ˋ(TAG, "fromJsonArrayStr JSONException: ", e2);
                    } catch (Throwable e22) {
                        Logger.ˋ(TAG, "fromJsonArrayStr JSONException: ", e22);
                    }
                }
                thirdUpdateActivityProtocol.ˊ(request);
                try {
                    this.callback.ˋ(new cjm("thirdappupdate.activity", thirdUpdateActivityProtocol), 1001);
                    return;
                } catch (ActivityNotFoundException e3) {
                    Logger.ˋ(TAG, "ActivityNotFoundException :" + e3.toString());
                    return;
                }
            }
            Logger.ˎ(TAG, "HiApp have no silentProcess");
            this.callback.setResult(6);
            this.callback.finish();
            return;
        }
        this.callback.finish();
    }

    public void onActivityResult(int i, int i2, Intent intent) {
        if (i == 1001) {
            Logger.ˎ(TAG, "onActivityResult: " + i2);
            this.callback.setResult(i2, intent);
            this.callback.finish();
        }
    }

    public int onKeyDown(int i, KeyEvent keyEvent) {
        if (i == 4 && keyEvent.getRepeatCount() == 0) {
            this.callback.setResult(4, null);
        }
        return super.onKeyDown(i, keyEvent);
    }

    public void cancelTask() {
        super.cancelTask();
        this.callback.setResult(7, null);
    }

    public void onDestroy() {
        super.onDestroy();
        this.callback.ˊ().finishActivity(1001);
    }
}
